<template>
  <div class="goodinfo-container">
    <!-- 小球 -->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="isShow" ref="ball"></div>
    </transition>

    <!-- 图片展示区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunboList="lunboList" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <!-- 价格区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsinfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价:
            <del class="old-price">${{ goodsinfo.market_price }}</del>&nbsp;&nbsp;
            销售价:
            <span class="new-price">${{ goodsinfo.sell_price }}</span>
          </p>
          <p>
            购买数量:
            <numbox @getCount="getSelectCount" :max="goodsinfo.stock_quantity"></numbox>
          </p>
          <mt-button type="primary">立即购买</mt-button>
          <mt-button type="danger" @click="addToShopCar">加入购物车</mt-button>
        </div>
      </div>
    </div>

    <!-- 详细信息 -->
    <div class="mui-card">
      <div class="mui-card-header">商品标题</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号: {{ goodsinfo.goods_no }}</p>
          <p>库存情况: {{ goodsinfo.stock_quantity }}</p>
          <p>上架时间: {{ goodsinfo.add_time | dateFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="getDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="getCom(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
import swiper from "../subcomps/swiper.vue";
import numbox from "../subcomps/goodinfo-numbox.vue";

export default {
  data() {
    return {
      id: this.$route.params.id,
      lunboList: [],
      goodsinfo: [],
      isShow: false,
      selectedCount: 1
    };
  },
  created() {
    this.getList();
    this.getGoodsInfo();
  },
  methods: {
    getList() {
      this.$http.get("getthumimages/" + this.id).then(res => {
        if (res.body.status === 0) {
          res.body.message.forEach(item => {
            item.img = item.src;
          });
          this.lunboList = res.body.message;
        } else {
          Toast("获取图片失败！");
        }
      });
    },
    getGoodsInfo() {
      this.$http.get("goods/getinfo/" + this.id).then(res => {
        if (res.body.status === 0) {
          this.goodsinfo = res.body.message[0];
        }
      });
    },
    getDesc(id) {
      this.$router.push({ name: "goodDesc", params: { id } });
    },
    getCom(id) {
      this.$router.push({ name: "goodCom", params: { id } });
    },
    addToShopCar() {
      this.isShow = !this.isShow;
      var goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsinfo.sell_price,
        selected: true
      }
      this.$store.commit('addToCar',goodsinfo)

    },
    beforeEnter(el) {
      el.style.transform = "translate(0, 0)";
    },
    enter(el, done) {
      el.offsetWidth;
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();
      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;
      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all 0.6s cubic-bezier(.4, -0.3, 1, 0.68)";
      done();
    },
    afterEnter(el) {
      this.isShow = !this.isShow;
    },
    getSelectCount(count) {
      this.selectedCount = count;
      console.log(this.selectedCount)
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.goodinfo-container {
  margin-bottom: 50px;
  background-color: #eee;
  overflow: hidden;
  .price {
    .new-price {
      color: #f00;
      font-weight: bold;
      font-size: 15px;
    }
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 10px;
    }
  }
  .ball {
    position: absolute;
    width: 15px;
    height: 15px;
    background-color: #f00;
    border-radius: 50%;
    z-index: 99;
    left: 140px;
    top: 390px;
  }
}
</style>